package com.android.pdd.baseapp.hometab;

import android.os.Bundle;
import android.view.View;


import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.android.pdd.baseapp.R;
import com.android.pdd.baseapp.base.BaseActivity;
import com.android.pdd.baseapp.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Li&Meng on 2018/8/30.
 * 底部tabs架构基类
 * --- 子类有两种：容器+Fragment+底部Tabs  和  ViewPager+Fragment+底部Tabs
 */
public abstract class BaseBottomTabActivity extends BaseActivity {
    protected View centerView;
    protected List<BaseFragment> fragments = new ArrayList<>();
    protected BottomTabView bottomTabView;
    protected boolean isCenterViewSkipNewAty; //点击CenterView后是否是跳转到新的Activity
    protected FragmentManager fragmentManager;

    private List<TabItemView> tabItemViewList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        fragmentManager = getSupportFragmentManager();
        initFragments();
        initTabViews();
        super.onCreate(savedInstanceState);
        bottomTabView = findViewById(R.id.bottomTabView);
        bottomTabView.setOnSecondSelectListener(new BottomTabView.OnSecondSelectListener() {
            @Override
            public void onSecondSelect(int position) {
                onTabContinuousClick(position);
            }
        });
        setCenterView();
        init();
    }

    public BottomTabView getBottomTabView() {
        return bottomTabView;
    }

    public void switchTab(int position) {
        bottomTabView.switchTab(position);
    }

    /**
     * 需要设置CenterView的话，就重写该方法
     * 并且在方法中调用带参数的setCenterView方法
     */
    protected void setCenterView() {

    }

    protected final View setCenterView(int centerViewLayout, boolean isCenterViewSkipNewAty, View.OnClickListener clickListener) {
        this.isCenterViewSkipNewAty = isCenterViewSkipNewAty;
        View centerView = getLayoutInflater().inflate(centerViewLayout, bottomTabView, false);
        if (isCenterViewSkipNewAty) {
            if (clickListener != null) {
                centerView.setOnClickListener(clickListener);
            }
        } else {
            centerView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCenterViewClick(v);
                }
            });
        }
        return this.centerView = centerView;
    }

    protected View getCenterView() {
        return this.centerView;
    }

    /**
     * 显示tab标签上tip消息的数量
     *
     * @param tabIndex
     * @param tipNumber
     */
    protected void showTipNumber(int tabIndex, int tipNumber) {
        bottomTabView.showTipNumber(tabIndex, tipNumber);
    }

    protected void showTipPoint(int tabIndex) {
        bottomTabView.showTipPoint(tabIndex);
    }

    /**
     * 隐藏tab标签上的tip消息
     *
     * @param tabIndex
     */
    protected void hideTipNumber(int tabIndex) {
        bottomTabView.hideTipNumber(tabIndex);
    }

    protected void hideTipPoint(int tabIndex) {
        bottomTabView.hideTipPoint(tabIndex);
    }

    protected abstract void init();

    protected abstract void initTabViews();

    protected abstract void initFragments();

    protected List<TabItemView> getTabViews() {
        return tabItemViewList;
    }

    protected void addTabView(TabItemView tabItemView) {
        tabItemViewList.add(tabItemView);
    }

    protected void addFragment(BaseFragment baseFragment) {
        fragments.add(baseFragment);
    }
//
//    protected abstract List<BaseFragment> getFragments();

    protected abstract void onCenterViewClick(View centerView);

    //连续选中
    protected abstract void onTabContinuousClick(int position);

    protected BaseFragment getFragment(int tabIndex) {
        if (fragments != null && fragments.size() > tabIndex) {
            return fragments.get(tabIndex);
        }
        throw new RuntimeException("Fragments is null or tabIndex out of size");
    }

    protected abstract BaseFragment getCurrentFragment();
}
